package com.performax.database.Dao;


import java.util.Date;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import com.performax.database.Vo.*;
import com.performax.database.oh2game.mapper.GlobalMapper;
import com.performax.database.oh2game.mapper.AccountInfoMapper;
import com.performax.utils.*;

@Service(value = "oh2gameGlobalDao")
public class Oh2GameGlobalDao {
	
	@Resource(name = "globalMapper")
	private	GlobalMapper globalMapper;
	
	@Resource(name = "accountInfoMapper")
	private	AccountInfoMapper accountInfoMapper;
	
	
	public AppInfoVo getAppInfo(){
		return this.globalMapper.getAppInfo();
	}
	
	public String	getMarketUrl(){
		return this.globalMapper.getMarketUrl();
	}
	
	public AccountInfoVo getAccountInfo(String userId){
		return this.accountInfoMapper.getAccountInfo(userId);
	}
	public AccountInfoVo getSdkAccountInfo(HashMap<String, Object> userParam){
		return this.accountInfoMapper.getSdkAccountInfo(userParam);
	}
	
	public String getAccountAuthMailInfo(String userId){
		return this.accountInfoMapper.getAccountAuthMailInfo(userId);
	}
	
	public List<UserAuthInfoVo> getAccountAuthkey(HashMap<String, Object> param){
		return this.accountInfoMapper.getAccountAuthkey(param);
	}
	public List<UserAuthInfoVo> getSdkAccountAuthkey(HashMap<String, Object> param){
		return this.accountInfoMapper.getSdkAccountAuthkey(param);
	}
	
	public void updAuthKey(HashMap<String, Object> authKey){
		this.accountInfoMapper.updAuthKey(authKey);
	}
	
	public void updAuthKeyExpireDt(HashMap<String, Object> authKey){
		this.accountInfoMapper.updAuthKeyExpireDt(authKey);
	}
	
	/*public AccountInfoVo getAccountInfoByPhone(String phone){
		return this.accountInfoMapper.getAccountInfoByPhone(phone);
	}*/
	
	public int checkExistUser(String userId){
		return this.accountInfoMapper.checkExistUser(userId);
	}
	
	public void	regAccount(AccountInfoVo	accountVo, HashMap<String, Object>	hUserDeviceInfo){
		this.accountInfoMapper.insertUserInfo(accountVo);
		this.accountInfoMapper.insertProfile(accountVo);
		this.accountInfoMapper.insertUserDevice(hUserDeviceInfo);
	}
	
	public void	regSdkAccount(AccountInfoVo	accountVo, HashMap<String, Object>	hUserDeviceInfo){
		this.accountInfoMapper.insertSdkUserInfo(accountVo);
		this.accountInfoMapper.insertProfile(accountVo);
		this.accountInfoMapper.insertSdkUserDevice(hUserDeviceInfo);
	}
	
	public void regUserDevice(HashMap<String, Object>	hUserDeviceInfo){
		this.accountInfoMapper.insertUserDevice(hUserDeviceInfo);
	}
	
	public String getFileSavePath(String type){
		//HashMap<String, Object> prop	=	this.globalMapper.getFileSavePath("dev");
		if(Oh2gameUtils.GLOBAL_SETTING	==	null){
			Oh2gameUtils.GLOBAL_SETTING	=	this.globalMapper.getFileSavePath(Oh2gameUtils.serverOpType);
		}
		//HashMap<String, Object> prop	=	this.globalMapper.getFileSavePath(Oh2gameUtils.serverOpType);
		
		return (String)Oh2gameUtils.GLOBAL_SETTING.get(type);
	}
	
	public HashMap<String, Object> getEmailAuthInfo(String uid){
		
		long	expireTerm	=	1000*60*60*3;	//	3hour
		/*HashMap<String, String>	info	=	this.globalMapper.getEmailTemplate(Oh2gameUtils.serverOpType);
		
		
		String	template	=	(String)info.get("EMAIL_AUTH_TEMPLATE");
		String	title		=	(String)info.get("EMAIL_AUTH_TITLE");
		String	domain		=	(String)info.get("SERVICE_DOMAIN");
		String	port		=	(String)info.get("SERVICE_PORT");*/
		String	template	=	getFileSavePath("EMAIL_AUTH_TEMPLATE");
		String	title		=	getFileSavePath("EMAIL_AUTH_TITLE");
		String	domain		=	getFileSavePath("SERVICE_DOMAIN");
		String	port		=	getFileSavePath("SERVICE_PORT");
		
		Date	expireDt	=	new	Date(System.currentTimeMillis()+expireTerm);
		SimpleDateFormat	format	=	new	SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String	validDate	=	format.format(expireDt);
		String	authKey		=	Oh2gameUtils.getAuthKey(uid, validDate);
		
		template	=	template.replaceAll("oh2gameUserId",uid);
		//template	=	template.replaceAll("authLink", 	"http://"+domain+":"+port+"/performax/app/api/extra/emailAuth?akey="+authKey+"&uid="+uid);
		template	=	template.replaceAll("authLink", 	"http://"+domain+"/app/api/extra/emailAuth?akey="+authKey+"&uid="+uid);
		template	=	template.replaceAll("validDate", 	validDate);
		
		
		HashMap<String, Object>	authInfo	=	new	HashMap<String,Object> ();
		authInfo.put("title", 		title);
		authInfo.put("contents",	template);
		authInfo.put("expireDt", 	validDate);
		authInfo.put("authKey", 	authKey);
		authInfo.put("authType", 	"JOIN_EMAIL");
		authInfo.put("userId", 		uid);
		
		return authInfo;
	}
	
	public void insertEmailAuthInfo(HashMap<String, Object> param){
		this.globalMapper.insertAuthInfo(param);
	}
	
	
	public void regPushTarget(HashMap<String, Object> pushParam){
		this.globalMapper.regPushTarget(pushParam);
	}
	
	public HashMap<String, Object> getUserEmailAuthInfo(HashMap<String, Object> param){
		return this.globalMapper.getUserEmailAuthInfo(param);
	}
	
	public void completeUserAuth(HashMap<String, Object> param){
		this.globalMapper.setUserStatus(param);
		this.globalMapper.setUserAuthTp(param);
	}
	
	public void updRegId(HashMap<String, Object> param){
		this.accountInfoMapper.updateUserPushKey(param);
	}
	
	public void	insertSmsAuthNo(HashMap<String, Object> param){
		this.globalMapper.insertAuthInfo(param);
		this.globalMapper.smsAuthNoSend(param);
	}
	
	public HashMap<String, Object> getAuthNo(String phone){
		return this.accountInfoMapper.getSmsAuthNo(phone);
	}
	
	public void	completeSmsAuth(String phone, String authNo){
		this.accountInfoMapper.completeSmsAuth(phone, authNo);
	}
	
	public void updatePassword(HashMap<String, Object> param){
		this.accountInfoMapper.updatePassword(param);
	}
	
	public int checkDuplicateNickName(HashMap<String, Object> param){
		return this.accountInfoMapper.checkDuplicateNickName(param);
	}
	
	public void changeNickName(AccountInfoVo accountInfo){
		this.accountInfoMapper.changeNickName(accountInfo);
	}
}
